<?php //m('node')->display_search()->display('html');?>
<div id="node_edit">
<!--<h1><?php $this->write('type');?> edit</h1>-->
<form accept-charset="utf-8" name="node_form" action="" method="post" class="box" onsubmit="return node_submit();">
<div><input class="submit" type="submit" name="submit"  value="save" /> <input class="submit" name="submit" type="submit" value="save & view" /></div>
<table style="width: 99%">
<tbody>
<tr>
    <td valign="top">Caption:<br /><input class="text" name="caption" type="text" value="<?php $this->write('caption');?>" onchange="caption_changed();"/></td>
    <td rowspan="3" width="150px">
        <input type="hidden" id="node_thumbnail" name="node_thumbnail" value="" />
        <?php if ($this->has_data('thumbnail')): ?>
            <?php echo tos_img($this->data['thumbnail']['src'], $this->data['thumbnail']['alt'], 145, 0, array('onclick' => "node_thumbnail_edit(this)"));?>
        <?php else:?>
            <img width="145" onclick="node_thumbnail_edit(this)" src="<?php echo tos_cdn_url('img/icon/big-blueprint.png', 'sys');?>" />
        <?php endif;?>
    </td>
</tr>
<tr>
    <td valign="top">Url segment:<br /><input class="text" name="url_segment" type="text" value="<?php $this->write('url_segment');?>" onkeyup="url_sugment_customise();"/></td>
</tr>
<tr>
    <td valign="top">Keywords:<br /><input class="text" name="keywords" type="text" value="<?php $this->write('keywords');?>" /></td>
</tr>
<tr>
    <td valign="top" colspan="2">Description:<br /><textarea id="description" style="height:120px;" name="description"><?php $this->write('description');?></textarea></td>
</tr>
</tbody>
</table>
<script type="text/javascript" src="<?php echo tos_cdn_url('js/z.js', 'sys');?>"></script>
    <div id="node_content">
    <?php $this->display_box(View::DEFAULT_BOX, 'html');?>
    </div>
    <div>Is public: <input name="is_public" type="checkbox" style="vertical-align: middle;" /></div>
    <div><input class="submit" type="submit" name="submit"  value="save" /> <input class="submit" name="submit" type="submit" value="save & view" /></div>
</form>
</div>
<script>
function node_thumbnail_edit(target) {
    var caption = document.forms['node_form']['caption'].value;
    var path = '<?php echo tos_get_config('node_thumbnail_path');?>';
    z.uploadImg(path, caption, 
	    target.src,
	    function () { 
    	   target.src = this.src; 
    	   z.s("node_thumbnail").value = this.osrc;
    	}
    );
}
function node_submit() {
    var form = document.forms['node_form'];
    var caption = trim(form.elements['caption'].value);
    if (caption === '') {
        alert('Caption cannot be none');
        return false;
    }

    return true;
}
function caption_changed() {
    var form = document.forms['node_form'];
    var url_segment = form.elements['url_segment'];
    var url_segment_str = trim(url_segment.value);
    if ( ! url_segment.customised) {
        var caption_str = trim(form.elements['caption'].value);
        var url_segment_str = caption_str.replace(/[^a-z0-9]+/ig, '-');
        url_segment.value = url_segment_str.toLowerCase();
    }
}
function url_sugment_customise() {
    var url_segment = document.forms['node_form']['url_segment'];
    var url_segment_value = trim_left(url_segment.value);
    if (url_segment_value !== '') {
        url_segment.customised = true;
        url_segment.value = url_segment_value.replace(/[^a-z0-9]+/ig, '-').toLowerCase();
    }
    else {
    	url_segment.customised = false;
        caption_changed();
    }
}
function trim(str) {
	return trim_right(trim_left(str));
}
function trim_left(str) {
    return str.replace(/^\s\s*/, ''); 
}
function trim_right(str) {
    return str.replace(/\s\s*$/, '');
}
</script>